package com.svpino.netflix.parsers;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.svpino.netflix.ProxyImpl;
import com.svpino.netflix.model.Feature;


public abstract class AbstractFeatureParser<T extends Feature> extends ListParser<T> {

	public AbstractFeatureParser(ProxyImpl connection) {
		super(connection);
	}

	@Override
	public List<T> list(Element... elements) {
		Element element = elements[0];
		
		List<T> list = new ArrayList<T>();

		NodeList nodeList = element.getElementsByTagName(getElementName());
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element itemElement = (Element) nodeList.item(i);
			
			String term = ((Element) itemElement).getAttribute("term");
			list.add(createItem(itemElement, term));
		}

		return list;
	}
	
	protected abstract String getElementName();
	
	protected abstract T createItem(Element element, String term);

}
